package dry.example.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * @author zc
 */
@RestController
public class AController {

    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @PostConstruct
    public void run() {
        System.out.println("I am tom");
        run2();
        run3();
    }

    public void run2() {
        try {
            RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("admin/01").methods(RequestMethod.GET).build();
            Method method = AController.class.getMethod("h01", HttpServletRequest.class, Long.class, String.class);
            requestMappingHandlerMapping.registerMapping(requestMappingInfo, this, method);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run3() {

    }

    public Object h01(HttpServletRequest request, @RequestParam("id") Long id, @RequestParam("name") String name) {
        return String.format("id=%d name=%s", id, name);
    }

}
